﻿using UnityEngine;
using UnityEngine.EventSystems;

namespace TowerDefence
{
    public class BuildNode : MonoBehaviour
    {
        GameManager gameManager;

        [HideInInspector]
        public Color startColor; // 初始颜色
        public Color activeColor; // 激活颜色
        [HideInInspector]
        public Renderer rend; // 渲染器

        [HideInInspector]
        public Tower towerScript; // 塔的脚本
        [HideInInspector]
        public GameObject tower; // 塔的游戏对象
        [HideInInspector]
        public TowerData towerBlueprint; // 塔的蓝图
        [HideInInspector]
        public bool isUpgraded = false; // 是否已升级
        [HideInInspector]
        public int upgradeIndex = 0; // 升级索引
        [Space]
        public Vector3 offset = new Vector3(-1.5f, 0, -1.5f); // 偏移量
        [HideInInspector]
        public bool isSelected; // 是否被选中

        private void Start()
        {
            gameManager = GameManager.instance; // 获取GameManager的实例

            rend = GetComponent<Renderer>(); // 获取组件
            offset = new Vector3(-1.5f, 0, -1.5f); // 设置偏移量
            startColor = rend.material.color; // 保存初始颜色
        }

        public void SetColor(Color color)
        {
            rend.material.color = color; // 设置颜色
        }

        public Vector3 GetBuildPosition()
        {
            return transform.position + offset; // 获取建造位置
        }

        public void UpgradeTurret01()
        {
            // 防止在最终升级后继续升级
            if (upgradeIndex >= 5 || towerBlueprint.towerPrefab == null)
                return;

            if (towerBlueprint.upgrade01 == null)
                return;

            if (PlayerStats.Gold < towerBlueprint.upgrade01.cost)
            {
                Debug.Log("Not enough gold to upgrade that!"); // 输出金币不足的信息
                return;
            }

            PlayerStats.Gold -= towerBlueprint.upgrade01.cost; // 扣除升级花费的金币

            // 销毁旧的塔
            Destroy(tower);

            // 建造新的塔
            GameObject _tower = Instantiate(towerBlueprint.upgrade01.towerPrefab, GetBuildPosition(), Quaternion.identity);
            tower = _tower;
            towerScript = _tower.GetComponent<Tower>();

            towerBlueprint = towerBlueprint.upgrade01;
            // 添加升级效果

            upgradeIndex++;

            if (upgradeIndex >= 5)
                isUpgraded = true;

            rend.material.color = startColor; // 恢复初始颜色
        }


        public void UpgradeTurret02()
        {
            // 防止在最终升级后继续升级
            if (upgradeIndex >= 5 || towerBlueprint.towerPrefab == null)
                return;

            if (towerBlueprint.upgrade02 == null)
                return;

            if (PlayerStats.Gold < towerBlueprint.upgrade02.cost)
            {
                Debug.Log("金币不足，无法升级！");
                return;
            }

            PlayerStats.Gold -= towerBlueprint.upgrade02.cost;

            // 销毁旧塔
            Destroy(tower);

            // 构建新塔
            GameObject _tower = Instantiate(towerBlueprint.upgrade02.towerPrefab, GetBuildPosition(), Quaternion.identity);
            tower = _tower;
            towerScript = _tower.GetComponent<Tower>();

            towerBlueprint = towerBlueprint.upgrade02;
            // 添加升级效果

            upgradeIndex++;

            if (upgradeIndex >= 5)
                isUpgraded = true;

            rend.material.color = startColor;
        }

        public void SellTower()
        {
            PlayerStats.Gold += towerBlueprint.sellValue;

            // 出售效果

            Destroy(tower);
            towerBlueprint = null;
            isUpgraded = false;
        }

        private void OnMouseEnter()
        {
            if (EventSystem.current.IsPointerOverGameObject())
                return;

            if (Utils.Utilities.IsPointerOverUIObject())
                return;

            rend.material.color = activeColor;
        }

        private void OnMouseDown()
        {
            if (EventSystem.current.IsPointerOverGameObject())
                return;

            if (Utils.Utilities.IsPointerOverUIObject())
                return;

            rend.material.color = activeColor;
            isSelected = true;

            gameManager.SelectBuildGround(this);
        }


        private void OnMouseExit()
        {
            // 如果鼠标位于UI对象上，则返回
            if (EventSystem.current.IsPointerOverGameObject())
                return;

            // 如果鼠标位于UI对象上，则返回
            if (Utils.Utilities.IsPointerOverUIObject())
                return;

            // 如果未被选择，恢复颜色为初始颜色
            if (isSelected == false)
                rend.material.color = startColor;
        }

    }
}
